# Package Maker for Dragon Script.
# Copyright (C) 2010 Hajime Yoshimori <RoarLudros@gmail.com>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


set(XZ_COMMAND   "@XZ_COMMAND@")
set(BIN_DIR      "@CMAKE_CURRENT_BINARY_DIR@")
set(SRC_DIR      "@CMAKE_CURRENT_SOURCE_DIR@")
set(BUILD_TYPE   "@CMAKE_BUILD_TYPE@")

file(REMOVE_RECURSE "${BIN_DIR}/DragonScript-@PROJECT_STABLE_VERSION@")
file(REMOVE         "${BIN_DIR}/DragonScript-@PROJECT_STABLE_VERSION@.tar.xz")
# file(MAKE_DIRECTORY "${BIN_DIR}/DragonScript-@PROJECT_STABLE_VERSION@")

if(NOT "@ADDITIONAL_EXCLUDE_FILES@" STREQUAL "")
  set(EXCLUDE_FILES "@ADDITIONAL_EXCLUDE_FILES@")
endif(NOT "@ADDITIONAL_EXCLUDE_FILES@" STREQUAL "")
set(EXCLUDE_FILES "${EXCLUDE_FILES}"
				  "[^/]*\\.[bB][aA][kK]"
				  "[^/]*~"
				  "\\#[^/]*\\#"
				  "Doxyfile"
				  "CMakeCache\\.txt"
				  "spec-[^/]*\\.cmake"
				  "config.h"
				  "Makefile"
				  "install_manifest\\.txt"
				  "make_dist\\.cmake"
				  "cmake_(un)?install\\.cmake"
				  "[^/]*\\.[tT][aA][gG]"
				  "INSPIRATION"
				  "libdragonscript\\.pc"
				  "[^/]*\\.ilg"
				  "[^/]*\\.idx"
				  "[^/]*\\.out"
				  "[^/]*\\.log"
				  "[^/]*_srcs\\.txt"
				  "[^/]*\\.upd"
				  "src/cdragon"
				  "src/gdragon"
				  "src/kdragon")
set(EXCLUDE_DIRS  "lib"
				  "bin"
				  "share"
				  "locale"
				  "LC_MESSAGES"
				  "html"
				  "\\.svn"
				  "CMakeFiles"
				  "CMakeTmp")

set(EXCLUDE_PATTERN_P "")
foreach(EXCLUDE_FILE ${EXCLUDE_FILES})
  set(EXCLUDE_PATTERN_P "${EXCLUDE_PATTERN_P}.*/${EXCLUDE_FILE}|")
endforeach(EXCLUDE_FILE)
foreach(EXCLUDE_DIR  ${EXCLUDE_DIRS})
 set(EXCLUDE_PATTERN_P "${EXCLUDE_PATTERN_P}.*/${EXCLUDE_DIR}$|")
endforeach(EXCLUDE_DIR)
string(REGEX REPLACE "\\|$" "" EXCLUDE_PATTERN ${EXCLUDE_PATTERN_P})

file(COPY "${SRC_DIR}/" DESTINATION "${BIN_DIR}/DragonScript-@PROJECT_STABLE_VERSION@"
						PATTERN "DragonScript-@PROJECT_STABLE_VERSION@" EXCLUDE
						REGEX ${EXCLUDE_PATTERN} EXCLUDE)

message(STATUS "Making Tarball...")
execute_process(COMMAND "${CMAKE_COMMAND}" "-E" "tar" "cvf"
						"${BIN_DIR}/DragonScript-@PROJECT_STABLE_VERSION@.tar"
						"DragonScript-@PROJECT_STABLE_VERSION@"
				WORKING_DIRECTORY "${BIN_DIR}"
				RESULT_VARIABLE   RES1
				OUTPUT_VARIABLE   OUT)
if(NOT RES1)
  execute_process(COMMAND "${XZ_COMMAND}" "-z" "${BIN_DIR}/DragonScript-@PROJECT_STABLE_VERSION@.tar"
                  WORKING_DIRECTORY "${BIN_DIR}"
                  RESULT_VARIABLE   RES2)
  if(RES2)
	message(FATAL_ERROR "Compressing Failed.")
  endif(RES2)
else(NOT RES1)
  message(FATAL_ERROR "Archiving Failed.")
endif(NOT RES1)

if(BUILD_TYPE STREQUAL "Debug")
  message(STATUS "Compressed files are:\n${OUT}")
else(BUILD_TYPE STREQUAL "Debug")
  file(REMOVE_RECURSE "${BIN_DIR}/DragonScript-@PROJECT_STABLE_VERSION@")
endif(BUILD_TYPE STREQUAL "Debug")

message(STATUS "Making Tarball... Done")

